package jsyf.oa.web.psi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.psi.PurchaseFacade;
import jsyf.oa.web.socket.MyWebSocketHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.socket.TextMessage;

import java.util.List;

/**
 * Created by Maqiuqiu on 2018/10/20.
 */
@Api(value = "进销存-申购接口", description = "进销存-申购接口")
@Controller
@RequestMapping("/purchase")
public class PurchaseController {
    @Resource
    private PurchaseFacade purchaseFacade;
    @Autowired
    private MyWebSocketHandler myWebSocketHandler;

    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = this.purchaseFacade.save(jsonData);
        //发送消息
        sendMessage(jsonObject.getString("message"));
        jsonObject.remove("message");
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonObject));
    }

    @RequestMapping(value = "/updateBuyInfo.json", method = RequestMethod.POST)
    public void updateBuyInfo(HttpServletResponse response, @RequestBody String jsonData) {
        boolean result = purchaseFacade.updateBuyInfo(jsonData);
        if (result) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("提交失败"));
        }
    }

    @RequestMapping(value = "/updateState.json", method = RequestMethod.POST)
    public void updateState(HttpServletResponse response, @RequestBody String jsonData) {
        boolean result = purchaseFacade.updateState(jsonData);
        if (result) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("更新失败"));
        }
    }

    @RequestMapping(value = "/delById.json", method = RequestMethod.POST)
    public void delById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        boolean result = purchaseFacade.delById(jsonParam.getString("id"));
        if (result) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("删除失败！"));
        }
    }

    @RequestMapping(value = "/removeById.json", method = RequestMethod.POST)
    public void removeById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        Boolean result = purchaseFacade.removeById(jsonObject.getString("id"));
        JSONObject obj = new JSONObject();
        obj.put("success", result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(obj));
    }

    @RequestMapping(value = "/getPurchaseList.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "查询申购列表",
            notes = "查询申购列表",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    public void getPurchaseList(HttpServletResponse response,@RequestBody String jsonData) {
        JSONObject result = this.purchaseFacade.getPurchaseList(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(result));
    }

    @RequestMapping(value = "/getPurchaseById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "根据id获取申购信息",
            notes = "根据id获取申购信息",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData",value = "{id:''}",dataType = "String", paramType = "body")
    public void getPurchaseById(HttpServletResponse response,@RequestBody String jsonData) {
        JSONObject result = this.purchaseFacade.getPurchaseById(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(result));
    }

    @RequestMapping(value = "/applyAudit.json", method = RequestMethod.POST)
    public void applyAudit(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = purchaseFacade.applyAudit(jsonData);
        boolean flag = Boolean.parseBoolean(result.getString("flag"));
        //发送消息
        //sendMessage(result.getString("message"));
        if (flag) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("审核失败"));
        }
    }

    private void sendMessage(String jsonData) {
        List<JSONObject> jsonObjectList = null;
        if (!StringUtils.isBlank(jsonData)) {
            jsonObjectList = FastJsonConvert.convertJSONToArray(jsonData, JSONObject.class);
        }
        if (null != jsonObjectList && jsonObjectList.size() > 0) {
            TextMessage textMessage = new TextMessage(BaseUtils.succsessArrHourTimeRespond(jsonObjectList), true);
            try {
                myWebSocketHandler.sendMsgToAllUsers(textMessage);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
